<?php

/*
 * @copyright :     Ienter asia Technologies
 * @author :        Hunglv
 * @version         v.1.0
 * @created         20/6/2014
 * @Description :   Model leaveinfo 
 */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Mmod_leaveinfo extends MYAD_Model {

    protected $table_name = 'm_employeeleaveinfo';
    protected $table_employee = 'm_employee';

    function __construct() {
        parent::__construct();
    }

    /*
     * @author :        Hunglv
     * @created:        25/6/2014
     * @Description:    Get all data for leave info
     * @Input :         (Integer) $page
     * @Output :        (Array) data for view data
     */

    function getAllData($employeeID = 0, $page = 0, $employee = null) {
        if (!$employee) {
            redirect('/employee');
        }
        /* load all label */
        $labelData = $this->getLabelData();

        /* load all employee data range */
        $allData = $this->getLeaveinfor($employeeID);

        /* load pagination */
        $pagiData = array(
            'base_url' => base_url('employee/leave-info/'),
            'total_rows' => count($allData['data']),
            'uri_segment' => 3,
        );
        $pagination = $this->config->item('pagination');
        $offset = isset($pagiData['per_page']) ? $pagiData['per_page'] : $pagination['per_page'];
        $pagi = $this->getPagination($pagiData);

        /* load salaryinfo data range */
        $salaryData = $this->getLeaveinfor($employeeID, $page, $offset);

        /* get employee name */
        $employeeName = '';
        if ($employee) {
            $employeeName = $employee->EmployeeName;
        }

        $data = array(
            'show' => 'leaveinfolist',
            'pagi' => $pagi,
            'nullcharacter' => $this->lang->line('nullcharacter'),
            'message' => $this->lang->line('emp_li_table_empty_data'),
            'order' => $page,
            'employeeID' => $employeeID,
            'employeeName' => $employeeName,
        );
        return array_merge($data, $labelData, $salaryData);
    }

    /*
     * @author :        Hunglv
     * @created:        20/6/2014
     * @Description:    Get label data of leave info
     * @Input :         void
     * @Output :        (Array) data label
     */

    function getLabelData() {

        $title = $this->lang->line('emp_li_title');
        $lblemployeeName = $this->lang->line('emp_li_employee_name');
        $lblnewRegister = $this->lang->line('emp_li_new_register');
        $lblOrder = $this->lang->line('emp_li_table_col_order');
        $lblYear = $this->lang->line('emp_li_table_col_year');
        $lblAvailableLeave = $this->lang->line('emp_li_table_col_Available_leave');
        $lblAction = $this->lang->line('emp_li_table_action');
        $lblSpecialDate = $this->lang->line('emp_li_LeaveSpecialDate');

        $data = array(
            'title' => (empty($title) ? 'List Employee Leave Information' : $title),
            'lblemployeeName' => (empty($lblemployeeName) ? 'Employee name' : $lblemployeeName),
            'lblnewRegister' => (empty($lblnewRegister) ? 'New register' : $lblnewRegister),
            'lblOrder' => (empty($lblOrder) ? 'Order' : $lblOrder),
            'lblYear' => (empty($lblYear) ? 'Year' : $lblYear),
            'lblAvailableLeave' => (empty($lblAvailableLeave) ? 'Available leave' : $lblAvailableLeave),
            'lblAction' => (empty($lblAction) ? 'Action' : $lblAction),
            'lblSpecialDate' => (empty($lblSpecialDate) ? 'Special Date' : $lblSpecialDate),
        );
        return $data;
    }

    /*
     * @author :        Hunglv
     * @created:        
     * @Description:    Get leave info by employeeID
     * @Input :         
     *                  (Integer) $employeeID
     *                  (Integer) $limit
     *                  (Integer) $offset
     * @Output :        Array data /FALSE
     */

    function getLeaveinfor($employeeID = 0, $limit = Null, $offset = Null) {
        /* get salaryinfo */
        $this->db->from($this->table_name);
        $this->db->where('EmployeeId', $employeeID);
        if ($limit !== Null && $offset !== Null) {
            $this->db->limit($offset, $limit);
        } elseif ($limit !== NULL) {
            $this->db->limit($limit);
        }
        $query = $this->db->get();
        $result = $query->result();
        $temp = FALSE;
        if (count($result)) {
            $temp = $result;
        }

        return array('data' => $temp);
    }

    /*
     * @author :        Hunglv
     * @created:        20/6/2014
     * @Description:    check validate form add/edit employee
     * @Input :         void
     * @Output :        boolean
     */

    function validate() {
        $_POST['txtAvailableLeave'] = mb_convert_kana($_POST['txtAvailableLeave'], 'a');
        $_POST['txtSpecialDate'] = mb_convert_kana($_POST['txtSpecialDate'], 'a');
        $this->form_validation->set_error_delimiters('', '');
        $this->form_validation->set_rules('txtAvailableLeave', $this->lang->line('emp_li_table_col_Available_leave'), 'required|greater_than[0]|max_length[2]|numeric');
        $this->form_validation->set_rules('txtSpecialDate', $this->lang->line('emp_li_LeaveSpecialDate'), 'callback_validate_date|callback_min_date[cboYear]');
        return $this->form_validation->run();
    }

    /*
     * @author :        Hunglv
     * @created:        20/6/2014
     * @Description:    update leave info
     * @Input :         (Integer) $id
     * @Output :        boolean
     */

    function updateLeaveInfoData($id = 0, $year = Null) {
        
        if ($this->input->post('cboYear') != $year) {
            $conditionDuplicate = array(
                'EmployeeId' => $id,
                'Year' => (int)$this->input->post('cboYear'),
                'Del_Flag' => FALSE,
            );
            $duplicate = $this->checkDuplicate($this->table_name, $conditionDuplicate);
            if ($duplicate) {
                return array(
                    'flag' => FALSE,
                    'message' => $this->lang->line('emp_updateDB_duplicate')
                );
            }
        }

        $data = array(
            'EmployeeId' => $id,
            'AvailableLeave' => $this->input->post('txtAvailableLeave'),
            'Year' => $this->input->post('cboYear'),
            'SpecicalDate' => $this->input->post('txtSpecialDate'),
            'Del_Flag' => ($this->input->post('chkDelete')=='on') ? 1 : 0,
        );

        $condition = array();
        if (!empty($year)) {
            $condition['Year'] = $year;
            $condition['EmployeeId'] = $id;
        }
        $temp = $this->updateDB($this->table_name, $data, $condition);
        if ($temp) {
            return array(
                'flag' => TRUE,
                'message' => $this->lang->line('emp_updateDB_success')
            );
        } else {
            return array(
                'flag' => FALSE,
                'message' => $this->lang->line('emp_updateDB_fall')
            );
        }
    }

    /*
     * @author :        Hunglv
     * @created:        23/6/2014
     * @Description:    basic data for new add leave info
     * @Input :         (Interger) $id
     * @Input :         (Interger) $Year
     * @Output :        Array
     */

    function getAddLeaveInfo($id = 0, $Year = Null, $employee = false) {

        if (!$employee) {
            redirect('/employee');
        }
        /* get language info */
        $title = (is_null($Year)) ? $this->lang->line('emp_li_add_title') : $this->lang->line('emp_li_update_title');
        $lblemployeeName = $this->lang->line('emp_li_update_lblEmployeeName');
        $lblAvailableLeave = $this->lang->line('emp_li_table_col_Available_leave');
        $lblYear = $this->lang->line('emp_li_table_col_year');
        $btnBack = $this->lang->line('emp_li_back');
        $btnSubmit = $this->lang->line('emp_li_submit');
        $dialogTitle = $this->lang->line('emp_dialog_title');
        $lblSpecialDate = $this->lang->line('emp_li_LeaveSpecialDate');
        /* get employee info */
        $leaveInfo = $this->getLeaveInforByIDAndYear($id, $Year);        
        if ($id && !isset($_POST['btnSubmit']) && $leaveInfo) {
            /* is load default */
            $emp = array(
                'EmployeeleaveinfoId'=>$leaveInfo->EmployeeleaveinfoId,
                'cboYear' => $leaveInfo->Year,
                'txtAvailableLeave' => $leaveInfo->AvailableLeave,
                'txtSpecialDate' => $this->setDate($leaveInfo->SpecicalDate),
                'chkDelete' => $leaveInfo->Del_Flag,
            );
        } else {
            $emp = array(
                'cboYear' => $this->input->post('cboYear'),
                'txtAvailableLeave' => set_value('txtAvailableLeave'),
                'txtSpecialDate' => set_value('txtSpecialDate'),
                'chkDelete' => ($this->input->post('chkDelete')=='on') ? 1 : 0,
                'EmployeeleaveinfoId'=>   $this->input->post('txtEmployeeleaveinfoId') ,
            );
        }

        $temp = array(
            'js' => array('jquery-ui.js'),
            'css' => array('jquery-ui.css'),
            'show' => 'add_leaveinfo',
            /* set label */
            'title' => (empty($title) ? 'Add Employee Leave information' : $title),
            'employeeName' => (empty($employee) ? '' : $employee->EmployeeName),
            'dialogTitle' => (empty($dialogTitle) ? 'Notification' : $dialogTitle),
            'lblEmployeeName' => (empty($lblemployeeName) ? 'Employee' : $lblemployeeName),
            'lblAvailableLeave' => (empty($lblAvailableLeave) ? 'Available leave' : $lblAvailableLeave),
            'lblYear' => (empty($lblYear) ? 'Year' : $lblYear),
            'btnBack' => (empty($btnBack) ? 'Back' : $btnBack),
            'btnSubmit' => (empty($btnSubmit) ? 'Submit' : $btnSubmit),
            'lblSpecialDate' => (empty($lblSpecialDate) ? 'Special Date' : $lblSpecialDate),
            'minYear' => $this->session->userdata('minYear'),
            'maxYear' => $this->session->userdata('maxYear'),
            /* set data */
            'id' => $id,
        );
        return array_merge($temp, $emp);
    }

    /*
     * @author :        Hunglv
     * @created:        25/6/2014
     * @Description:    basic data for new add leave info
     * @Input :         (Interger) $id
     * @Input :         (Interger) $year
     * @Output :        Array
     * 
     */

    function getLeaveInforByIDAndYear($id = 0, $year = Null) {
        /* get salaryinfo */
        $this->db->from($this->table_name);
        $this->db->where('EmployeeId', $id);
        $this->db->where('Year', $year);
        $query = $this->db->get();
        $result = $query->result();
        if (count($result)) {
            return $result[0];
        } else {
            return FALSE;
        }
    }
    /*
     * @author :        Hunglv
     * @created:        
     * @Description:    Check exist
     * @Input :         (Interger) $year
     * @Input :         (Interger) $employeeID
     * @Output :        return true or false
     * 
     */

    function checkExist($employeeID,$year) {
        /* get salaryinfo */
        $this->db->from($this->table_name);
        $this->db->where('Year', $year);
        $this->db->where('EmployeeId', $employeeID);
        $query = $this->db->get();
        if ($query->num_rows()>0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
     /*
     * @author :        Quyet
     * @created:        20/6/2014
     * @Description:    get employee
     * @Input :         (Integer) $id
     * @Output :        object/false
     */

    function getEmployee($id = 0) { 
        $query = $this->db->get_where($this->table_employee, array('EmployeeId' => $id,'CompanyCode'=>$this->session->userdata('CompanyCode')));
        $result = $query->result();        
        if (count($result)) {
            return $result[0];
        } else {
            return FALSE;
        }
    }
}

?>
